package com.jx.lcs.user.web;

import com.jx.lcs.employee.entity.Employee;
import com.jx.lcs.user.entity.UserInfo;
import com.jx.lcs.user.service.UserInfoService;
import com.tianyu.jty.common.persistence.Page;
import com.tianyu.jty.common.web.BaseController;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import java.util.Map;

/** 
 * 理财师controller
 * @author JiaYY 
 * @date 2015年7月2日
 */
@Controller
@RequestMapping("user")
public class UserInfoController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(UserInfoController.class);

	@Autowired
	private UserInfoService userinfoService;
	
	/**
	 * 默认页面
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "bUserInfo/bUserInfoList";
	}
	
	/**
	 * 获取理财师列表json
	 */
	@RequestMapping(value="json",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> userInfoList(HttpServletRequest request) {
		try{
			Page<UserInfo> page = getPage(request);
//			List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
			page = userinfoService.findUserInfoList(page, request);
			//page = userinfoService.search(page, filters);
			return getEasyUIData(page);
		}catch(Exception e){
			logger.error("--获取用户列表出现异常！", e);
			return null;
		}
	}
	/**
	 * 修改用户权限跳转
	 *
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") String id, Model model) {
		model.addAttribute("userInfo", userinfoService.get(id));
		model.addAttribute("action", "update");
		return "bUserInfo/bUserInfoForm";
	}
	/**
	 * 修改用户权限
	 *
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public String update(HttpServletRequest request,Model model) {
		String id = request.getParameter("id");
		String marketShow = request.getParameter("marketShow");
		String lbsShow = request.getParameter("lbsShow");
		UserInfo userInfo = userinfoService.get(id);
		userInfo.setMarketShow(marketShow);
		userInfo.setLbsShow(lbsShow);
		userinfoService.update(userInfo);
		return "success";
	}
	/**
	 * 理财师身份审核数据查询
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "approve/{id}", method = RequestMethod.GET)
	public String approveForm(@PathVariable("id") String id, Model model) {
		model.addAttribute("userInfo", userinfoService.get(id));
		model.addAttribute("action", "approve");
		return "bUserInfo/bUserInfoApproveForm";
	}
	/**
	 * 修改理财师信息
	 * 
	 * @param employee
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "approve", method = RequestMethod.POST)
	@ResponseBody
	public String approve(@Valid @ModelAttribute @RequestBody UserInfo user, Model model) {
		try{
		
			UserInfo userInfo = userinfoService.get(user.getId());		
			if(userInfo != null) {					
				userInfo.setProfileStatus(user.getProfileStatus());
				userinfoService.update(userInfo);
			}
			return "success";
		}catch(Exception e){
			logger.error("--理财师身份审核出现异常！！", e);
			
			return "理财师身份审核失败";
		}
	}
}
